package com.koron.css2.projectCenter.impl;

import com.koron.css2.ApplicationConfig;
import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.BaseAccessoryMetadataBean;
import com.koron.css2.baseConfig.bean.BaseReceiptAccessoryBean;
import com.koron.css2.baseConfig.mapper.BaseAccessoryMetadataMapper;
import com.koron.css2.baseConfig.mapper.BaseReceiptAccessoryMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import com.koron.util.Upload;
import com.koron.util.encode.BASE64DecodedMultipartFile;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.web.multipart.MultipartFile;
import org.swan.bean.MessageBean;

import java.util.Map;


/**
 * 签字图片上传
 *
 * @author: zhongxj
 * @date: 2020-07-13 17:20
 **/

public class SignPicUpload implements ServerInterface {

    private static Logger log = LogManager.getLogger(SignPicUpload.class);


    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        // 交互机传过来仅有base64没有格式
        try {
            String path = "";
            Map<String, Object> map = JsonUtils.objectToPojo(req.getData(), Map.class);
            String billId  = (String) map.get("billId");
            Map<String,String> imgDataMap = (Map<String, String>) map.get("imgData");
            for (Map.Entry<String, String> entry : imgDataMap.entrySet()) {
                // 指纹没有的直接跳过
                if (StringUtils.isBlank(entry.getValue())) {
                    continue;
                }
                String key = entry.getKey();
                String value = entry.getValue();
                StringBuilder sb = new StringBuilder();
                if(key.equals("SignPdfBase64")){
                    //  签字pdf
                    sb.append("data:application/pdf;base64,");
                }else if(key.equals("SignNameBase64")){
                    // 签名图片
                    sb.append("data:image/png;base64,");
                }else if(key.equals("FingerPrintBase64")){
                    // 指纹图片
                    sb.append("data:image/png;base64,");
                }
                sb.append(value);
                MultipartFile file = BASE64DecodedMultipartFile.base64ToMultipart(sb.toString());
                path = Upload.upload(file);
                entry.setValue(ApplicationConfig.getAccessoryUploadUrl()+"fileDownload?path="+path);
                if (StringUtils.isNotBlank(path)) {
                    BaseAccessoryMetadataMapper metadataMapper = factory.getMapper(BaseAccessoryMetadataMapper.class);
                    BaseReceiptAccessoryMapper mapper = factory.getMapper(BaseReceiptAccessoryMapper.class);
                    BaseAccessoryMetadataBean metadata = new BaseAccessoryMetadataBean();
                    String metadataId = Tools.getObjectId();
                    metadata.setAccessoryPath(path);
                    metadata.setAccessoryName(FilenameUtils.getName(file.getOriginalFilename()));
                    metadata.setId(metadataId);
                    metadata.setAccessorySize(file.getSize());
                    metadata.setAccessoryType(file.getContentType());
                    metadata.setCreateAccount(userInfo.getUserInfo().getAcount());
                    metadata.setCreateName(userInfo.getUserInfo().getName());
                    metadata.setSourceFlag("1");
                    metadataMapper.insert(metadata);
                    BaseReceiptAccessoryBean bean = new BaseReceiptAccessoryBean();
                    bean.setId(Tools.getObjectId());
                    bean.setMetadataId(metadataId);
                    bean.setCreateAccount(userInfo.getUserInfo().getAcount());
                    bean.setCreateName(userInfo.getUserInfo().getName());
                    bean.setAccessoryName(FilenameUtils.getName(file.getOriginalFilename()));
                    bean.setReceiptId(billId);
                    // 附件类型
                    bean.setAccessoryNo("signature");
                    // 单据类型
                    bean.setReceiptType("PJ_INSTALL");
                    mapper.insert(bean);
                }
            }
            MessageBean<Map> msg = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "上传成功", Map.class);
            msg.setData(imgDataMap);
            return msg;
        } catch (Exception e) {
            logger.error("上传签字图片失败" + e.getMessage(), e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "上传失败", null);
        }
    }
}
